/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.menosehmais.framework.persistence;

import java.util.UUID;
import org.joda.time.DateTime;

/**
 *
 * @author Quintino
 */
public class Id  {

    /**
     * Ex:
     * UUID One: 067e6162-3b6f-4ae2-a171-2470b63dff00
     * UUID Two: 54947df8-0e9e-4471-a2f9-9af509fb5889
     */
    public static final String REGEX =
            "[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]"
            + "-[0-9a-f][0-9a-f][0-9a-f][0-9a-f]"
            + "-[0-9a-f][0-9a-f][0-9a-f][0-9a-f]"
            + "-[0-9a-f][0-9a-f][0-9a-f][0-9a-f]"
            + "-[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]";

    private String value;

    public Id(String id) throws IllegalArgumentException {
        this.setValue( id );
    }

    protected final void setValue(String id) throws IllegalArgumentException {
        UUID uuid = UUID.fromString( id );// Validando
        this.value = uuid.toString();
    }

    public boolean isValidUUID(){
        return Id.isValidUUID(value);
    }

    public static boolean isValidUUID(String value){
        UUID uuid;

        if(value != null)
        {
            try {
                uuid = UUID.fromString( value );

                DateTime dateTime = new DateTime();

                if( uuid.timestamp() < dateTime.getMillis() )
                {
                    return true;
                }

            } catch (Exception e) {
                //TODO: Logar erro.
            }
        }

        return false;
    }

    @Override
    public String toString() {
        return this.value;
    }

    public UUID toUUID() {
        return UUID.fromString( this.value );
    }

}
